package com.swizipuremvc.app.user.ui.view.main
{
	
	
	import com.swizipuremvc.app.user.ui.skin.main.MainSkin;
	import com.swizipuremvc.core.ApplicationFacade;
	import com.swizipuremvc.core.common.CommandType;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import spark.components.Button;
	import spark.components.TextInput;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class MainView extends SkinnableComponent
	{
		[Inject]
		public var facade:ApplicationFacade;
		
		[SkinPart(required="true")]
		public var userTextInput:TextInput;
		
		[SkinPart(required="true")]
		public var passwordInput:TextInput;
		
		
		[SkinPart(required="true")]
		public var sendBtn:Button;
		
		public function MainView()
		{
			super();
			this.setStyle("skinClass",MainSkin);
			this.addEventListener(Event.ADDED_TO_STAGE,creationCompleteHandler);
		}
		
		private function creationCompleteHandler(event:Event):void
		{
			this.facade.initialize();
			
		}
		
		private function authenticateHandler(event:MouseEvent):void
		{
			var params:ArrayCollection = new ArrayCollection();
			params.addItem(this.userTextInput.text);
			params.addItem(this.passwordInput.text);
			this.facade.sendNotification(CommandType.DASHBOARD_COMMAND.toString(),params);
		}	
		
		protected override function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName,instance);
			
			if(instance == sendBtn)
			{
				this.sendBtn.addEventListener(MouseEvent.CLICK,authenticateHandler);
			}
		}
		
		protected override function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName,instance);
			
			if(instance == sendBtn)
			{
				this.sendBtn.addEventListener(MouseEvent.CLICK,authenticateHandler);
			}
		}
	}
}